package com.pda.common.database;

import com.pda.common.PdaApp;
import com.pda.common.dao.BillInfoDao;
import com.pda.common.dao.CodeInfoDao;
import com.pda.common.dao.DealerInfoDao;
import com.pda.common.dao.ProductInfoDao;
import com.pda.common.dao.StoreInfoDao;
import java.util.ArrayList;
import java.util.List;

public class DataBaseControl {
    public static String TAG = "DataBaseControl";

    //region产品信息
    //增加产品
    public static long addProduct(ProductInfo entity) {
        long res = -1;
        try {
            res = PdaApp.daoSession.getProductInfoDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //删除全部产品
    public static void deleteProductAll() {
        try {
            PdaApp.daoSession.getProductInfoDao().deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询全部产品
    public static List<ProductInfo> getProductAll() {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        try {
            list = PdaApp.daoSession.getProductInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按名称查询产品
    public static List<ProductInfo> getProductID(String id) {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        try {
            list = PdaApp.daoSession.getProductInfoDao().queryBuilder().where(ProductInfoDao.Properties.Pid.like("%" + id + "%")).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按Name Code查询产品
    public static List<ProductInfo> getProductNC(String str) {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        try {
            list = PdaApp.daoSession.getProductInfoDao().queryBuilder().whereOr(ProductInfoDao.Properties.Code.like("%" + str + "%"),ProductInfoDao.Properties.Name.like("%" + str + "%")).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }
//endregion

    //region库房
    //增加库房
    public static long addStore(StoreInfo entity) {
        long res = -1;
        try {
            res = PdaApp.daoSession.getStoreInfoDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //删除全部库房
    public static void deleteStoreAll() {
        try {
            PdaApp.daoSession.getStoreInfoDao().deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询全部库房
    public static List<StoreInfo> getStoreAll() {
        List<StoreInfo> list = new ArrayList<StoreInfo>();
        try {
            list = PdaApp.daoSession.getStoreInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按名称查询库房
    public static List<StoreInfo> getStoreName(String name) {
        List<StoreInfo> list = new ArrayList<StoreInfo>();
        try {
            list = PdaApp.daoSession.getStoreInfoDao().queryBuilder().where(StoreInfoDao.Properties.Name.like("%" + name + "%")).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按ID查询库房
    public static List<StoreInfo> getStoreID(String id) {
        List<StoreInfo> list = new ArrayList<StoreInfo>();
        try {
            list = PdaApp.daoSession.getStoreInfoDao().queryBuilder().where(StoreInfoDao.Properties.Sid.like("%" + id + "%")).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }
//endregion

    //region经销商
    //增加经销商
    public static long addDealer(DealerInfo entity) {
        long res = -1;
        try {
            res = PdaApp.daoSession.getDealerInfoDao().insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //删除经销商
    public static void deleteDealerAll() {
        try {
            PdaApp.daoSession.getDealerInfoDao().deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询全部经销商
    public static List<DealerInfo> getDealerAll() {
        List<DealerInfo> list = new ArrayList<DealerInfo>();
        try {
            list = PdaApp.daoSession.getDealerInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按名称查询经销商
    public static List<DealerInfo> getDealerName(String name) {
        List<DealerInfo> list = new ArrayList<DealerInfo>();
        try {
            list = PdaApp.daoSession.getDealerInfoDao().queryBuilder().where(DealerInfoDao.Properties.Name.like("%" + name + "%")).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按ID查询经销商
    public static List<DealerInfo> getDealerID(String id) {
        List<DealerInfo> list = new ArrayList<DealerInfo>();
        try {
            list = PdaApp.daoSession.getDealerInfoDao().queryBuilder().where(DealerInfoDao.Properties.Did.like("%" + id + "%")).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

//endregion




    //region条码存储
    //增加条码
    public static long addCode(CodeInfo codeInfo) {
        long res = -1;
        try {
            res = PdaApp.daoSession.getCodeInfoDao().insert(codeInfo);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //删除全部条码
    public static void deleteCodeAll() {
        try {
            PdaApp.daoSession.getCodeInfoDao().deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除指定条码
    public static void deleteCode(CodeInfo codeInfo) {
        try {
            CodeInfo temp = PdaApp.daoSession.getCodeInfoDao().queryBuilder().where(CodeInfoDao.Properties.Type.eq(codeInfo.getType()) ,CodeInfoDao.Properties.Code.eq(codeInfo.getCode()) ).unique();
            PdaApp.daoSession.getCodeInfoDao().delete(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除指定条码
    public static void deleteCodes(List<CodeInfo> codeInfos) {
        try {
            PdaApp.daoSession.getCodeInfoDao().deleteInTx(codeInfos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询全部条码
    public static List<CodeInfo> getCodeAll() {
        List<CodeInfo> list = new ArrayList<CodeInfo>();
        try {
            list = PdaApp.daoSession.getCodeInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按条件查询条码
    public static List<CodeInfo> getCodeAllType(int type) {
        List<CodeInfo> list = new ArrayList<CodeInfo>();
        try {
            list = PdaApp.daoSession.getCodeInfoDao().queryBuilder().where(CodeInfoDao.Properties.Type.eq(type)).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //按条件查询条码
    public static CodeInfo getCodeAllStr(CodeInfo codeInfo) {
        CodeInfo mCodeInfo = null;
        try {
            mCodeInfo = PdaApp.daoSession.getCodeInfoDao().queryBuilder().where(CodeInfoDao.Properties.Type.eq(codeInfo.getType()),CodeInfoDao.Properties.Code.eq(codeInfo.getCode())).unique();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return mCodeInfo;
        }
    }


    //endregion

    //region本地订单
    //增加订单
    public static long addBill(BillInfo billInfo) {
        long res = -1;
        try {
            res = PdaApp.daoSession.getBillInfoDao().insert(billInfo);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return res;
        }
    }

    //查询全部订单
    public static List<BillInfo> getBillAll() {
        List<BillInfo> list = null;
        try {
            list = PdaApp.daoSession.getBillInfoDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //查询指定类型订单
    public static List<BillInfo> getBills(int type) {
        List<BillInfo> list = null;
        try {
            list = PdaApp.daoSession.getBillInfoDao().queryBuilder().where(BillInfoDao.Properties.Type.eq(type)).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }
    }

    //删除指定订单
    public static void deleteBill(BillInfo billInfo) {
        try {
            BillInfo temp = PdaApp.daoSession.getBillInfoDao().queryBuilder().where(BillInfoDao.Properties.Type.eq(billInfo.getType()) ,BillInfoDao.Properties.BillCode.eq(billInfo.getBillCode())).unique();
            PdaApp.daoSession.getBillInfoDao().delete(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //endregion
}


